/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PacketHandling;

import horizon.entity.Entity;
import horizon.entity.Player;
import horizon.network.EntityEncoder;
import horizon.network.Packet;
import horizon.states.GameState;
import horizon.states.GameTitleScreenState;
import horizon.states.StateManager;
import horizon.util.Vector2;
import java.util.StringTokenizer;

/**
 *
 * @author J_Laptop
 */
public class PacketParser {

    Packet packet;

    public PacketParser(Packet p) {

        packet = p;


        switch (classifyPacket()) {

            case SERV_REG:

                handleRegistPacket();

                break;

            case SERV_AUTH:

                handleAuthPacket();

                break;
            case SERV_DATA:

                handleDataPacket();

                break;

            case SERV_MAPDATA:

                handleMapData();

                break;
        }



    }

    private Packet.PKT_TYPE classifyPacket() {

        return packet.header;

    }

    private void handleAuthPacket() {
    }

    private void handleRegistPacket() {

        PacketHandler.getInstance().name = packet._data;
        //System.out.println(packet._data);
        ((GameTitleScreenState) StateManager.getInstance().getState(GameTitleScreenState.class)).clientAuthorized();


    }

    private void handleDataPacket() {

        String data = packet._data;
        StringTokenizer str = new StringTokenizer(data, ";");

        while (str.hasMoreTokens()) {

            String t = str.nextToken();
            evaluateCommand(t);

        }

    }

    private void handleMapData() {

        String data = packet._data;
        StringTokenizer str = new StringTokenizer(data, ";");

        String type = str.nextToken();

        if (type.equals("packetNumber")) {

            PacketHandler.getInstance().packetNumber = Integer.parseInt(str.nextToken());

        } else if (type.equals("entityList")) {

            while (str.hasMoreTokens()) {

                String t = str.nextToken();
                PacketHandler.getInstance().entityList.add(EntityEncoder.createEntity(t));
                //System.out.println(t);

            }

            PacketHandler.getInstance().packetRecieved++;

        } else if (type.equals("entity")) {

            while (str.hasMoreTokens()) {
                String t = str.nextToken();
                Entity e = EntityEncoder.createEntity(t);
                PacketHandler.getInstance().entityList.add(e);

                GameState game = ((GameState) StateManager.getInstance().getState(GameState.class));
                game.addChatLine(e.getId() + " has joined the game");


            }

        }



    }

    private void evaluateCommand(String t) {

        String opCode = t.substring(0, 3);

        switch (opCode) {

            case "mov":

                String cmd = t.substring(4, t.length());
                StringTokenizer str = new StringTokenizer(cmd, ":");
                String name = str.nextToken();
                String pos = str.nextToken();
                String mov = str.nextToken();


                float xPos = Float.valueOf(pos.substring(0, pos.lastIndexOf(",")));
                float yPos = Float.valueOf(pos.substring(pos.lastIndexOf(",") + 1));

                float xMov = Float.valueOf(mov.substring(0, mov.lastIndexOf(",")));
                float yMov = Float.valueOf(mov.substring(mov.lastIndexOf(",") + 1));

                Vector2 position = new Vector2(xPos, yPos);
                Vector2 v = new Vector2(xMov, yMov);

                GameState game = ((GameState) StateManager.getInstance().getState(GameState.class));
                Entity e = null;
                Vector2 curPos = null;
                if (name != null && game != null && game.m != null) {
                    e = game.m.getEntity(name);
                }

//                / && !name.equals(PacketHandler.getInstance().name;

                if (e != null) {

                    if (e instanceof Player) {

                        ((Player) e).movePlayer(v);

                    } else {
                        game.m.getEntity(name).move(v);
                    }

                    curPos = e.getPosition();
                }

                if ((!name.equals(PacketHandler.getInstance().name)) && curPos != null && ((Math.abs(curPos.getX() - position.getX()) > 10) || (Math.abs(curPos.getY() - position.getY())) > 10)) {

                    e.setPosition(position);

                } else if (name.equals(PacketHandler.getInstance().name) && curPos != null && ((Math.abs(curPos.getX() - position.getX()) > 25) || (Math.abs(curPos.getY() - position.getY())) > 25)) {

                    e.setPosition(position);

                }

                break;

            case "cha":
                
                String l = t.substring(4, t.length());
                StringTokenizer st = new StringTokenizer(l, ":");
                String n = st.nextToken();
                String msg = st.nextToken();
         
                GameState g = ((GameState) StateManager.getInstance().getState(GameState.class));
                g.addChatLine(n + ": " + msg);
                
                break;


        }

    }
}
